package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.common.util.RequestHolder;
import com.sec.etech.ism.model.IsmAppSetting;
import com.sec.etech.ism.model.IsmServiceDef;
import com.sec.etech.ism.service.*;
import com.sec.etech.ism.util.IsmStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class IsmGetChangeStepByMaterialListServiceImpl implements IsmMethodService {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    IsmAppSettingService ismAppSettingService;
    @Resource
    IsmServiceDefService ismServiceDefService;
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmTaskLogService ismTaskLogService;

    public IsmGetChangeStepByMaterialListServiceImpl(){}

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception{
        JSONObject result = new JSONObject();
        String msg = this.getChangeStepByMaterialList(sysCode, serviceCode, businessParams);
        result.put("flag", msg);
        result.put("msg", msg);
        return result;
    }

    private String getChangeStepByMaterialList(String sysCode, String serviceCode, JSONObject busObj){
        Map<String, String> logMap = new HashMap<>(5);
        //服务信息
        IsmServiceDef serviceDef = null;
        try{
            serviceDef = ismServiceDefService.getByCode(serviceCode);
            String appName = "getChangeStepByMaterialList";
            //调用的应用信息
            IsmAppSetting appSetting = ismAppSettingService.getByCode(sysCode);
            String url = appSetting.getAppAddress();
            //url = url.replace("http://10.9.70.139:1039/", "http://127.0.0.1:1039/");
            //先调用查询物料号的方法，检查物料号 /BAPI_MATERIAL_EXISTENCECHECK
            logMap.put("appName", appName);
            logMap.put("url", url);
            JSONObject mesParams = new JSONObject();
            mesParams.put("amendnum", busObj.getString("amendnum"));
            if(StringUtil.isNotEmpty(busObj.getString("fileid"))){
                mesParams.put("fileUrl", RequestHolder.getUrl() + "/sys/sysFile/preview.html?fileId=" +
                        busObj.getString("fileid") + "&withWatermark=1&audience=drawing");
            }else {
                String sql = "select amendid,fileid from proj_amend where amendid=?";
                List<Map<String, Object>> amendList = jdbcTemplate.queryForList(sql, busObj.getString("amendid"));
                if(amendList!=null && amendList.size()>0 && StringUtil.isNotEmpty(amendList.get(0).get("fileid"))){
                    mesParams.put("fileUrl", RequestHolder.getUrl() + "/sys/sysFile/preview.html?fileId=" +
                            amendList.get(0).get("fileid").toString() + "&withWatermark=1&audience=drawing");
                }else {
                    mesParams.put("fileUrl", "");
                }
            }
            Set<JSONObject> materialNoList = new HashSet<>();
            String sql = "select t2.material_no no1,t3.material_no no2 from proc_parts_tech_amend t1 " +
                    "left join spp_route_rev t2 on t1.target_id=t2.route_rev_id " +
                    "left join spp_route_rev t3 on t1.new_target_id=t3.route_rev_id where t1.amendid=?";
            List<Map<String, Object>> noList = jdbcTemplate.queryForList(sql, busObj.getString("amendid"));
            if(noList!=null && noList.size()>0){
                for(Map<String, Object> map:noList){
                    String no1 = IsmStringUtil.getStrNullToEmpty(map.get("no1"));
                    String no2 = IsmStringUtil.getStrNullToEmpty(map.get("no2"));
                    JSONObject materialNoObj = new JSONObject();
                    if(StringUtil.isNotEmpty(no1)){
                        materialNoObj.put("material_no", no1);
                    }else if (StringUtil.isNotEmpty(no2)){
                        materialNoObj.put("material_no", no2);
                    }
                    if(StringUtil.isNotEmpty(materialNoObj.getString("material_no"))){
                        materialNoList.add(materialNoObj);
                    }
                }
            }
            mesParams.put("materialNoList", materialNoList);
            ismCommonService.checkResult(url, mesParams.toJSONString(), serviceDef, null, busObj.toJSONString(), appName, logMap);
            return "success";
        }catch (Exception e){
            ismTaskLogService.saveExceptionTaskLog(serviceCode, serviceDef.getName(), logMap);
            log.error("工时信息推送MES接口异常。serviceDef.getCode()={}，serviceDef.getName()={}，logMap={}",
                    serviceDef.getCode(), serviceDef.getName(), logMap);
            e.printStackTrace();
            return "工时信息推送MES接口异常，请联系管理员处理！";
        }
    }

    @Override
    public String methodName(){
        return "getChangeStepByMaterialList";
    }
}
